const app = new Vue({
    el: '#perfect',
    data: function () {
        return {
            type: this.getParam(),
            man: {
                name: '',
                address: '',
                legal: '',
                uscc: '',
                contact: ''
            },
            sel: {
                name: '',
                type: '医院',
                legal: '',
                idNumber: '',
                address: ''
            },
            sup: {
                name: '',
                manager: '',
                address: ''
            }
        }
    },
    methods: {
        getParam: function () {
            let url = window.location.href;
            let p = url.split('?')[1];
            let params = new URLSearchParams(p);
            return params.get('type');
        },
        logout: function () {
            let token = window.localStorage.getItem('userToken');
            axios.get('/logout', {headers: {'Authorization': token}}).then(
                function (res) {
                    let code = res.data.code;
                    if (code === 200) {
                        console.log(res);
                        location.href = '../../index.html';
                    }
                }, function (err) {
                    console.log(err);
                }
            )
        },
        cancelPer: function () {
            this.man = {
                name: '',
                address: '',
                legal: '',
                uscc: '',
                contact: ''
            };
            this.sel = {
                name: '',
                type: '医院',
                legal: '',
                idNumber: '',
                address: ''
            };
            this.sup = {
                name: '',
                manager: '',
                address: ''
            };
            this.logout();
        },
        enterSys: function () {
            let token = window.localStorage.getItem('userToken');
            console.log(token);
            console.log(this.type);
            if (this.type === '1') {
                if (this.man.name === '') {
                    this.$message.warning('请填写企业名称！');
                    return;
                }
                if (this.man.address === '') {
                    this.$message.warning('请填写企业地址！');
                    return;
                }
                if (this.man.legal === '') {
                    this.$message.warning('请填写法定代表人！');
                    return;
                }
                if (this.man.uscc === '') {
                    this.$message.warning('请填写统一社会信用代码！');
                    return;
                }
                if (this.man.contact === '') {
                    this.$message.warning('请填写联系方式！');
                    return;
                }

                axios.post('/perfect/man', this.man,
                    {headers: {'Authorization': token}}).then(
                    function (res) {
                        console.log(res);
                        location.href = '../view/manufacturer/home.html';
                    }, function (err) {
                        console.log(err);
                    }
                )
            } else if (this.type === '2') {
                if (this.sel.name === '') {
                    this.$message.warning('请填写名称！');
                    return;
                }
                if (this.sel.type === '') {
                    this.$message.warning('请选择类型！');
                    return;
                }
                if (this.sel.legal === '') {
                    this.$message.warning('请填写负责人！');
                    return;
                }
                if (this.sel.idNumber === '') {
                    this.$message.warning('请填写负责人身份证号！');
                    return;
                }
                if (this.sel.address === '') {
                    this.$message.warning('请填写地址！');
                    return;
                }

                axios.post('/perfect/sel', this.sel,
                    {headers: {'Authorization': token}}).then(
                    function (res) {
                        console.log(res);
                        location.href = '../view/seller/home.html';
                    }, function (err) {
                        console.log(err);
                    }
                )
            } else if (this.type === '3') {
                if (this.sup.name === '') {
                    this.$message.warning('请填写部门名称！');
                    return;
                }
                if (this.sup.manager === '') {
                    this.$message.warning('请填写账号管理人！');
                    return;
                }
                if (this.sup.address === '') {
                    this.$message.warning('请填写部门属地！');
                    return;
                }

                axios.post('/perfect/sup', this.sup,
                    {headers: {'Authorization': token}}).then(
                    function (res) {
                        console.log(res);
                        location.href = '../view/supervision/home.html';
                    }, function (err) {
                        console.log(err);
                    }
                )
            }
        }
    },
});